package tools.Jalview;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

import tools.blat.PslParser;
import tools.hmmer.hmmerParser;
import tools.phobius.PhobiusParser;
import tools.phobius.PhobiusShortAlignment;

public class JalviewUtils {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		if(args.length>0){
			if(args[0].equals("BLATFeatureFileProt")&&args.length>1){
				ArrayList<String> files=new ArrayList<String>();
				for(int i=1;i<args.length;i++){
					files.add(args[i]);
				}
				BLATFeatureFile(files,true);
			}else if(args[0].equals("BLATFeatureFileNucl")&&args.length>1){
				ArrayList<String> files=new ArrayList<String>();
				for(int i=1;i<args.length;i++){
					files.add(args[i]);
				}
				BLATFeatureFile(files,false);
			}else if(args[0].equals("PhobiusFeatureFile")&&args.length>1){
				ArrayList<String> files=new ArrayList<String>();
				for(int i=1;i<args.length;i++){
					files.add(args[i]);
				}
				PhobiusFeatureFile(files);
			}else if(args[0].equals("hmmerFeatureFile")&&args.length>1){
				ArrayList<String> files=new ArrayList<String>();
				for(int i=1;i<args.length;i++){
					files.add(args[i]);
				}
				hmmerFeatureFile(files);
			}else{
				System.err.println(printHelp());
				System.exit(616);
			}
		}else{
			System.err.println(printHelp());
			System.exit(616);
		}
		System.exit(0);
	}
	
	private static void hmmerFeatureFile(ArrayList<String> files) throws Exception{
		ArrayList<Feature> features=new ArrayList<Feature>();
		BufferedReader in;
		String[] l;
		for (String file : files) {
			in=new BufferedReader(new FileReader(file));
			in.readLine();
			for(String s=in.readLine();s!=null;s=in.readLine()){
				l=s.split("\t");
				if(l.length>=9){
					features.add(new Feature(l[7],l[0],l[3],l[3],-1,Integer.parseInt(l[1]),Integer.parseInt(l[2])));
				}
			}
		}
		FeatureFileGenerator ffg=new FeatureFileGenerator();
		System.out.println(ffg.FeatureString(features));
	}

	private static void PhobiusFeatureFile(ArrayList<String> files) throws Exception{
		ArrayList<Feature> features=new ArrayList<Feature>();
		PhobiusParser pp;
		for (String file : files) {
			pp=new PhobiusParser(new BufferedReader(new FileReader(file)));
			for(;pp.hasMoreAlignments();){
				features.addAll(pp.nextAlignment().toJalviewTMFeature());
			}
		}
		FeatureFileGenerator ffg=new FeatureFileGenerator();
		System.out.println(ffg.FeatureString(features));
	}

	private static void BLATFeatureFile(ArrayList<String> files,boolean prot) throws Exception{
		ArrayList<Feature> features=new ArrayList<Feature>();
		PslParser pp;
		for (String file : files) {
			pp=new PslParser(new BufferedReader(new FileReader(file)));
			for(;pp.hasMoreAlignments();){
				features.addAll(pp.nextAlignment().toJalviewSpliceFeatures(prot));
			}
		}
		FeatureFileGenerator ffg=new FeatureFileGenerator();
		System.out.println(ffg.FeatureString(features));
	}

	public static String printHelp() {
		String help="JalviewUtils contains different methods for manipulation in association to Jalview\n";
		help+="Usage: JalviewUtils <cmd> <input>\n";
		help+="where <cmd> is:\n";
		help+="BLATFeatureFileProt - generates a featurefile, with aminoacid distance, from a set of BLAT-files\n";
		help+="\t<input> = <BLAT-file> ... <BLAT-file>\n";
		help+="BLATFeatureFileNucl - generates a featurefile, with nucleotide distance, from a set of BLAT-files\n";
		help+="\t<input> = <BLAT-file> ... <BLAT-file>\n";
		help+="PhobiusFeatureFile - generates a featurefile from a set of phobius-files\n";
		help+="\t<input> = <phobius-file> ... <phobius-file>\n";
		help+="hmmerFeatureFile - generates a featurefile from a set of hmmer-files (tabulated)\n";
		help+="\t<input> = <hmmer-file> ... <hmmer-file>\n";
		
		return help;
	}
}
